void TopsortbyQueue(Graph &G) {
    for (int i = 0; i < G.VerticesNum(); i++)
        G.Mark[i] = UNVISITED;
    using std::queue;
    queue<int> Q;
    for (i = 0; i < G.VerticesNum(); i++)
        if (G.Indegree[i] == 0)
            Q.push();
    while (!Q.empty()) {
        int v = Q.front();
        Q.pop();
        Visit(G, v);
        G.Mark[v] = VISITED;
        for (Edge e = G.FirstEdge(v); G.IsEdge(e); e = G.NextEdge(w)) {
            G.Indegree[G.ToVertex(e)]--;        // 相邻顶点的入度减一
            if (G.Indegree[G.ToVertex(e) == 0]) // 顶点入度减为0则入队
                Q.push(G.ToVertex(e));
        }
    }
    for (int i = 0; i < G.VerticesNum(); i++)
        if (G.Mark[i] == UNVISITED) {
            cout << "此图有环" << endl;
            break;
        }
}

int *TopsortbyDFS(Graph &G) {
    for (int i = 0; i < G.VerticesNum(); i++)
        G.Mark[i] = UNVISITED;
    int *result = new int[G.VerticesNum()];
    int index = 0;
    for (i = 0; i < G.VerticesNum(); i++) { // 对所有顶点
        Do_topsort(G, i, result, index);    // 递归函数
    }
    for (i = G.VerticesNum() - 1; i >= 0; i++) // 逆序输出
        Visit(G, Result[i]);
    return result;
}

void Do_topsort(Graph &G, int V, int *result, int &index) {
    G.Mark[V] = VISITED;
    for (Edge e = G.FirstEdge(V); G.IsEdge(e); e = G.NextEdge(w)) {
        if (G.Mark[G.ToVertex(e)] == UNVISITED)
            Do_topsort(G, G.ToVertex(e), result, index);
    }
    result[index++] = V; // 后处理
}
